/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dataanalysis.main;

import dataanalysis.ASMODE;
import dataanalysis.CLSMODE;
import dataanalysis.Flow;
import dataanalysis.Model;
import java.util.Date;
import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ASSearch;
import weka.attributeSelection.semiAS.SemiAttributeSelector;
import weka.classifiers.Classifier;
import weka.core.Instances;
import weka.filters.Filter;

/**
 *
 * @author Administrator
 */
public final class MainDocument {

    private final MainApp m_manager;
    private Instances m_curData = null;//当前待处理的数据
    private Instances m_oriData = null;//原始数据
    private Instances m_preData = null;//预处理后的数据
    private Instances m_selectData = null;//特征选择后的数据
    private Instances m_editData = null;//编辑修改筛选后的数据
    private Filter m_filter = null;//预处理方法
    private ASEvaluation m_evaluator = null;//评价方法
    private ASSearch m_searcher = null;//搜索策略
    private Classifier m_classifier = null;//分类器
    private SemiAttributeSelector m_semiASer = null;//半监督特征选择方法
    private String m_fullName = "";
    private String m_strASResult = "";
    private String m_strClaasifierResult = "";
    private String m_strPridictResult = "";
    private Flow m_processFlow = new Flow();
    private int m_resultIndex = 0;
    private int m_curDataIndex = 0;
    private CLSMODE m_clsMode = CLSMODE.CROSS;
    private int m_clsSeed = 0;
    private ASMODE m_ASMode = ASMODE.FULL;
    private int m_ASFoldNum = 10;
    private int m_ASSeed = 1;
    private int m_clsFoldNum = 10;
    private double m_clsSplitPercent = 66;
    private Model m_serialModel = null;
    private String m_txtModel = "";

    public MainDocument(final MainApp manager) {
        m_manager = manager;
    }

    public Instances getCurInstances() {
        return m_curData;
    }

    public void setCurInstances(Instances data) {
        if (data == null) {
            return;
        }
        if (m_curData != null) {
            m_curData.delete();
        }
        m_curData = new Instances(data);
        m_manager.updateAllCapabilitiesFilter();
    }

    public Instances getOriInstances() {
        return m_oriData;
    }

    public void setOriInstances(Instances data) {
        if (data == null) {
            return;
        }
        if (m_oriData != null) {
            m_oriData.delete();
        }
        m_oriData = new Instances(data);
    }

    public Instances getPreInstances() {
        return m_preData;
    }

    private void setPreInstances(Instances data) {
        if (data == null) {
            return;
        }
        if (m_preData != null) {
            m_preData.delete();
        }
        m_preData = new Instances(data);
    }

    public void updatePreInstances() {
        setPreInstances(m_curData);
    }

    public Instances getSelectInstances() {
        return m_selectData;
    }

    private void setSelectInstances(Instances data) {
        if (data == null) {
            return;
        }
        if (m_selectData != null) {
            m_selectData.delete();
        }
        m_selectData = new Instances(data);
    }

    public void updateSelectInstances() {
        setSelectInstances(m_curData);
    }

    public Instances getEditInstances() {
        return m_editData;
    }

    public void setEditInstances(Instances data) {
        if (data == null) {
            return;
        }
        if (m_editData != null) {
            m_editData.delete();
        }
        m_editData = new Instances(data);
    }

    public void openNewInstances(Instances data) {
        this.setCurInstances(data);
        this.setOriInstances(data);
        this.m_curDataIndex = 0;
        this.m_preData = null;
        this.m_selectData = null;
        this.m_editData = null;
        this.m_strASResult = "";
        this.m_strClaasifierResult = "";
        this.m_strPridictResult = "";
    }

    public Filter getFilterValue() {
        return m_filter;
    }

    public ASEvaluation getASEvaluationValue() {
        return m_evaluator;
    }

    public ASSearch getASSearchValue() {
        return m_searcher;
    }

    public Classifier getClassifierValue() {
        return m_classifier;
    }

    SemiAttributeSelector getSemiASValue() {
        return m_semiASer;
    }

    public void setFilterValue(Filter filter) {
        m_filter = filter;
    }

    public void setClassifierValue(Classifier classifier) {
        m_classifier = classifier;
    }

    public void setASEvaluationValue(ASEvaluation evaluator) {
        m_evaluator = evaluator;
    }

    public void setASSearchValue(ASSearch searcher) {
        m_searcher = searcher;
    }

    public void setSemiASValue(SemiAttributeSelector semiASer) {
        m_semiASer = semiASer;
    }

    public CLSMODE getClsMode() {
        return m_clsMode;
    }

    public int getClsSeed() {
        return m_clsSeed;
    }

    public void setClsSeed(int seed) {
        m_clsSeed = seed;
    }

    public void setClsMode(CLSMODE clsMode) {
        m_clsMode = clsMode;
    }

    public void setFullFileName(String filename) {
        m_fullName = filename;
    }

    public String getFileName() {
        String fileName = m_fullName.substring(m_fullName.lastIndexOf("\\") + 1);
        return fileName;
    }

    private String getDateString() {
        String str = "\n日期: " + new Date().toString();
        str += "\n";
        str += "-----------------------------------------------------------------------";
        str += "-----------------------------------------------------------------------";
        str += "\n\n";
        return str;
    }

    public void setASResult(String str) {
        m_strASResult += "\n===特征选择===\n";
        m_strASResult += getDateString();
        m_strASResult += str;
    }

    public void setClassifierResult(String str) {
        m_strClaasifierResult += "\n===分类器===\n";
        m_strClaasifierResult += getDateString();
        m_strClaasifierResult += str;
    }

    public void setPredictResult(String str) {
        m_strPridictResult += "\n===预测模型===\n";
        m_strPridictResult += getDateString();
        m_strPridictResult += str;
    }

    public String getASResult() {
        return m_strASResult;
    }

    public String getClassifierResult() {
        return m_strClaasifierResult;
    }

    public String getPredictResult() {
        return m_strPridictResult;
    }

    public void setProcessFlowValue(int a, int b, int c) throws Exception {
        m_processFlow.setValue(a, b, c);
    }

    public boolean isPreFlow() {
        return m_processFlow.isPre();
    }

    public boolean isSupASFlow() {
        return m_processFlow.isSupAS();
    }

    public boolean isSemiASFlow() {
        return m_processFlow.isSemiAS();
    }

    public boolean isClassifierFlow() {
        return m_processFlow.isClassifier();
    }

    public void setResultIndex(int index) {
        m_resultIndex = index;
    }

    public int getResultIndex() {
        return m_resultIndex;
    }

    public void setCurDataIndex(int index) throws Exception {
        if (index >= 0 && index < 4) {
            m_curDataIndex = index;
        } else {
            throw new Exception("Data index out of range!");
        }
    }

    public void selectCurData() {
        switch (m_curDataIndex) {
            case 0:
                this.setCurInstances(m_oriData);
                break;
            case 1:
                this.setCurInstances(m_editData);
                break;
            case 2:
                this.setCurInstances(m_preData);
                break;
            case 3:
                this.setCurInstances(m_selectData);
                break;
        }
    }

    public ASMODE getASMode() {
        return m_ASMode;
    }

    public int getASFoldNum() {
        return m_ASFoldNum;
    }

    public int getASSeed() {
        return m_ASSeed;
    }

    public int getClsFoldNum() {
        return m_clsFoldNum;
    }

    public double getClsSplitPercent() {
        return m_clsSplitPercent;
    }

    public void setASMode(ASMODE ASMode) {
        m_ASMode = ASMode;
    }

    public void setASFoldNum(int ASFoldNum) {
        m_ASFoldNum = ASFoldNum;
    }

    public void setASSeed(int ASSeed) {
        m_ASSeed = ASSeed;
    }

    public void setClsFoldNum(int clsFoldNum) {
        m_clsFoldNum = clsFoldNum;
    }

    public void setClsSplitPercent(double clsSplitPercent) {
        m_clsSplitPercent = clsSplitPercent;
    }

    public String getTxtModel() {
        return m_txtModel;
    }

    public Model getSerialModel() {
        return m_serialModel;
    }

    public void setModel(Model model) {
        m_serialModel = model;
        m_txtModel = model.toString();
    }

}
